/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-26
 * Time: 16:56
 */
import java.util.Stack;

//棒球比赛
public class Test {
    public static void main(String[] args) {
        String[] str = {"5","2","C","D","+"};
        System.out.println(calPoints(str));
    }
    public static int  calPoints(String[] ops) {
        int size = 0;
        int sum = 0;
        Stack<Integer> stack = new Stack<>();
        for(int i=0;i<ops.length;i++){
            String str = ops[i];
            if(isString(str)==false){
                stack.push(Integer.parseInt(str));
                size++;
            }else{
                if(size>=1 && str.equals("C")){
                    stack.pop();
                    size--;
                }
                else if(size>=1 && str.equals("D")){
                    int top = stack.peek();
                    stack.push(top*2);
                    size++;
                }
                else if(size>=2 && str.equals("+")){
                    int top1 = stack.pop();
                    int top2 = stack.pop();
                    stack.push(top2);
                    stack.push(top1);
                    stack.push(top1+top2);
                    size++;
                }
            }
        }
        while(size>0){
            sum+=stack.pop();
            size--;
        }
        return sum;
    }
    public static boolean isString(String str){
        if(str.equals("D")||str.equals("C")||str.equals("+")){
            return true;
        }
        return false;
    }
}
